﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FOModels.Card;
using Bussiness.Card;
using Bussiness.Product;
using FOModels.Product;
using System.Net;
using Bussiness.ProductFAQ;
using FOModels.ProductFAQ;
using FOModels.Language;
using Bussiness.Language;
using FOSystem.CultureHelper;
using Bussiness.Status;
using FOModels.Status;
using Bussiness.Company;
using FOModels.Company;
using System.Net.Mail;
using System.Configuration;

namespace FO.Controllers
{
    public class SavingFuelCardController : CultureController
    {
        //
        // GET: /SavingFuelCard/

        public ActionResult Index()
        {
            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            ProductBLL probll = new ProductBLL();
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ViewBag.CardProperties = cardbll.getCardPropertiesList(lang.LangID);
            ViewBag.Fuel = cardbll.getCreditCardList(SavingFuelCardID, lang.LangID);
            ViewBag.ProDes = probll.getProductByID(SavingFuelCardID, lang.LangID);
            return View();
        }
        public ActionResult CardsByProvider(int CompanyID)
        {
            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductBLL probll = new ProductBLL();
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            ViewBag.CardProperties = cardbll.getCardPropertiesList(lang.LangID);
            ViewBag.Fuel = cardbll.getCreditCardByProvider(SavingFuelCardID, CompanyID, lang.LangID);
            ViewBag.ProDes = probll.getProductByID(SavingFuelCardID, lang.LangID);

            return View();

        }
        public ActionResult CardRelati(int CompanyID, int CardID)
        {
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CreditCard> list = new List<CreditCard>();
            list = cardbll.getcreditcardrelati(SavingFuelCardID, CompanyID, CardID, lang.LangID);
            return PartialView(list);
        }
        public ActionResult Detail(int CardID)
        {
            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            CreditCard card = new CreditCard();
            ProductFAQBLL faqbll = new ProductFAQBLL();
            card = cardbll.getCreditCardByID(CardID, lang.LangID);
            ViewBag.CardRelati = cardbll.getcreditcardrelati(card.CardCategoryID, card.CompanyID, card.CardID, lang.LangID);
            ViewBag.FAQ = faqbll.getfaqbyproid(card.CardCategoryID, lang.LangID);
            return View(card);
        }
        [HttpPost]
        public ActionResult getcardbycom_pro(int CreditCardProperties, int CompanyID)
        {
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CreditCard> list = new List<CreditCard>();

            list = cardbll.getcardbycom_pro(SavingFuelCardID, CreditCardProperties, CompanyID, lang.LangID);

            return Json(list);
        }
        [HttpPost]
        public ActionResult getCreditCardListAllByProvider(int CompanyID)
        {
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CreditCard> list = new List<CreditCard>();

            list = cardbll.getCreditCardByProvider(SavingFuelCardID, CompanyID, lang.LangID);

            return Json(list);
        }
        [Authorize(Roles = "8,9")]
        public ActionResult ShowCase()
        {
            return View();
        }
        [HttpPost]
        public ActionResult getFuelByProperties(int CreditCareProperties)
        {
            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CreditCard> list = new List<CreditCard>();
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            list = cardbll.getCreditCardListByProperties(SavingFuelCardID, CreditCareProperties, lang.LangID);
            return Json(list);
        }
        [HttpPost]
        public ActionResult getFuelListAll()
        {
            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CreditCard> list = new List<CreditCard>();
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            list = cardbll.getCreditCardList(SavingFuelCardID, lang.LangID);

            return Json(list);
        }

        public ActionResult SavingFuelCardInfo(int CardID)
        {
            CardBLL cardbll = new CardBLL();
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            ProductFAQBLL faqbll = new ProductFAQBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            CreditCard list = new CreditCard();

            list = cardbll.getCreditCardByID(CardID, lang.LangID);
            ViewBag.FAQ = faqbll.getfaqbyproid(SavingFuelCardID, lang.LangID);
            Fcom.IDLib.SaveMoneyID.AccountsynClient client = new Fcom.IDLib.SaveMoneyID.AccountsynClient();
            FOModels.User.User us = null;
            if (User.Identity.Name != "")
            {
                Fcom.IDLib.SaveMoneyID.FullUserInfo fullinfo = client.getUserInfo(User.Identity.Name);
                us = new FOModels.User.User();
                us.DisplayName = fullinfo.Fullname;
                us.Email = fullinfo.Email;
                us.GroupID = fullinfo.GroupID;
                us.Status = fullinfo.Status;
                us.Username = fullinfo.Username;
                us.Phone = fullinfo.Phone;
                us.DOB = fullinfo.DateOfBirth;

            }
            ViewBag.UserInfo = us;
            return View(list);
        }

        [Authorize(Roles = "8,9")]
        public ActionResult ManagerFuelCard()
        {
            return View();
        }
        public JsonResult getFuelCard()
        {
            CardBLL cardbll = new CardBLL();
            List<CreditCard> list = new List<CreditCard>();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);

            list = cardbll.getcardtomanager(2, lang.LangID);

            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult Articles()
        {
            ProductBLL cardbll = new ProductBLL();
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductModels card = new ProductModels();
            card = cardbll.getProductByID(SavingFuelCardID, lang.LangID);
            Models.Product c = new Models.Product();
            c.Description = WebUtility.HtmlDecode(card.Description);
            return View(c);
        }
        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult Articles(FO.Models.Product card)
        {

            ProductBLL cardbll = new ProductBLL();
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductModels ca = new ProductModels();
            ca = cardbll.getProductByID(SavingFuelCardID, lang.LangID);
            if (ca != null)
            {
                ca.Description = WebUtility.HtmlEncode(card.Description);
                cardbll.UpdateProductDesriptionLang(ca, lang.LangID);
                return RedirectToAction("ManagerFuelCard");
            }

            return View();


        }
        [Authorize(Roles = "8,9")]
        public ActionResult FAQShowCase()
        {
            return View();
        }
        [Authorize(Roles = "8,9")]
        public ActionResult ManagerFAQ()
        {
            return View();
        }
        public JsonResult getFAQJson()
        {
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            ProductFAQBLL cardbll = new ProductFAQBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<ProductFAQModels> list = new List<ProductFAQModels>();

            list = cardbll.getfaqbyproid(SavingFuelCardID, lang.LangID);

            return Json(list, JsonRequestBehavior.AllowGet);
        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertFAQ()
        {

            return View();

        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertFAQ(FO.Models.ProductFAQInsert card)
        {
            ProductFAQBLL cardbll = new ProductFAQBLL();
            int SavingFuelCardID = int.Parse(System.Configuration.ConfigurationManager.AppSettings["SavingFuelCardID"].ToString());
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            if (ModelState.IsValid)
            {
                ProductFAQModels items = new ProductFAQModels();
                items.FAQTitles = card.FAQTitles;
                items.FAQAnwers = card.FAQAnwers;
                items.ProductID = SavingFuelCardID;
                cardbll.InsertFAQ(items);
                ProductFAQModels faqitems = cardbll.gettop1faq();
                ProductFAQModels itemsLang = new ProductFAQModels();
                items.ProductFAQID = faqitems.ProductFAQID;
                itemsLang.ProductFAQID = faqitems.ProductFAQID;
                cardbll.InsertFAQLang(items, 1);
                itemsLang.FAQTitles = card.FAQTitlesLang;
                itemsLang.FAQAnwers = card.FAQAnwersLang;
                cardbll.InsertFAQLang(itemsLang, 2);
                return RedirectToAction("ManagerFAQ");
            }
            return View(card);

        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateFAQ(int ProductFAQID)
        {
            ProductFAQBLL cardbll = new ProductFAQBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductFAQModels card = new ProductFAQModels();

            card = cardbll.getfaqbyid(ProductFAQID, lang.LangID);

            Models.ProductFAQ c = new Models.ProductFAQ();
            c.FAQTitles = card.FAQTitles;
            c.FAQAnwers = card.FAQAnwers;
            return View(c);
        }
        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateFAQ(FO.Models.ProductFAQ card)
        {
            ProductFAQBLL cardbll = new ProductFAQBLL();
            ProductFAQModels ca = new ProductFAQModels();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);

            ca = cardbll.getfaqbyid(card.ProductFAQID, lang.LangID);
            if (ca != null)
            {
                ca.FAQTitles = card.FAQTitles;
                ca.FAQAnwers = card.FAQAnwers;
                cardbll.UpdateFAQ(ca, lang.LangID);
                return RedirectToAction("ManagerFAQ");
            }

            return View();


        }

        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteFAQ(int ProductFAQID)
        {
            ProductFAQBLL bll = new ProductFAQBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            ProductFAQModels card = new ProductFAQModels();
            card = bll.getfaqbyid(ProductFAQID, lang.LangID);
            if (card != null)
            {
                bll.DeleteFAQ(card.ProductFAQID);
                var result = new { Status = "OK" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Status = "Xóa thất bại" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }

        }

        [Authorize(Roles = "8,9")]
        public ActionResult Contract()
        {
            StatusBLL status = new StatusBLL();
            List<StatusModels> liststatus = new List<StatusModels>();
            liststatus = status.getStatus();
            ViewBag.StatusID = liststatus;
            return View();
        }
        public JsonResult getfuelcardContract()
        {

            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CardApplyModels> list = new List<CardApplyModels>();
            list = cardbll.getcontractall(2);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [Authorize(Roles = "8,9")]
        public ActionResult ContractNew()
        {
            StatusBLL status = new StatusBLL();
            List<StatusModels> liststatus = new List<StatusModels>();
            liststatus = status.getStatus();
            ViewBag.StatusID = liststatus;
            return View();
        }
        public JsonResult getfuelcardContractNew()
        {

            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CardApplyModels> list = new List<CardApplyModels>();
            list = cardbll.getcontractbycateid(2, 1);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [Authorize(Roles = "8,9")]
        public ActionResult ContractProcessing()
        {
            StatusBLL status = new StatusBLL();
            List<StatusModels> liststatus = new List<StatusModels>();
            liststatus = status.getStatus();
            ViewBag.StatusID = liststatus;
            return View();
        }
        public JsonResult getfuelcardContractProcessing()
        {

            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CardApplyModels> list = new List<CardApplyModels>();
            list = cardbll.getcontractbycateid(2, 2);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [Authorize(Roles = "8,9")]
        public ActionResult ContractProcessed()
        {
            StatusBLL status = new StatusBLL();
            List<StatusModels> liststatus = new List<StatusModels>();
            liststatus = status.getStatus();
            ViewBag.StatusID = liststatus;
            return View();
        }
        public JsonResult getfuelcardContractProcessed()
        {

            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CardApplyModels> list = new List<CardApplyModels>();
            list = cardbll.getcontractbycateid(2, 3);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [Authorize(Roles = "8,9")]
        public ActionResult ContractSpam()
        {
            StatusBLL status = new StatusBLL();
            List<StatusModels> liststatus = new List<StatusModels>();
            liststatus = status.getStatus();
            ViewBag.StatusID = liststatus;
            return View();
        }
        public JsonResult getfuelcardContractSpam()
        {

            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CardApplyModels> list = new List<CardApplyModels>();
            list = cardbll.getcontractbycateid(2, 4);
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public JsonResult getfuelcontractbystatus(int StatusID)
        {

            CardBLL cardbll = new CardBLL();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            List<CardApplyModels> list = new List<CardApplyModels>();
            list = cardbll.getcontractbycateid(2, StatusID);
            return Json(list, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult ContractDetail(int CardApplyID)
        {
            CardBLL cardbll = new CardBLL();
            CardApplyModels cardapp = new CardApplyModels();
            cardapp = cardbll.getcontractbyid(CardApplyID);
            StatusBLL status = new StatusBLL();
            List<StatusModels> liststatus = new List<StatusModels>();
            liststatus = status.getStatus();
            FO.Models.CardApplyModels c = new FO.Models.CardApplyModels();
            ViewBag.StatusListID = new SelectList(liststatus, "StatusID", "StatusName", cardapp.StatusID);
            c.StatusID = cardapp.StatusID;
            c.CustomerName = cardapp.CustomerName;
            c.CustomerEmail = cardapp.CustomerEmail;
            c.CustomerDOB = cardapp.CustomerDOB;
            c.CustomerPhone = cardapp.CustomerPhone;
            c.CustomerIncome = cardapp.CustomerIncome;
            c.Cost = cardapp.Cost;
            c.Interest = cardapp.Interest;
            c.CompanyName = cardapp.CompanyName;
            c.CardImage = cardapp.CardImage;
            c.StatusName = cardapp.StatusName;
            c.CardName = cardapp.CardName;
            c.CardID = cardapp.CardID;
            c.AppDate = cardapp.AppDate;
            return View(c);
        }
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "8,9")]
        public ActionResult ContractDetail(FO.Models.CardApplyModels card)
        {
            CardBLL cardbll = new CardBLL();
            CardApplyModels cardapp = new CardApplyModels();
            cardapp = cardbll.getcontractbyid(card.CardApplyID);
            StatusBLL status = new StatusBLL();
            List<StatusModels> liststatus = new List<StatusModels>();
            liststatus = status.getStatus();
            ViewBag.StatusListID = new SelectList(liststatus, "StatusID", "StatusName", cardapp.StatusID);
            if (cardapp != null)
            {
                cardapp.StatusID = card.StatusID;
                cardbll.UpdateStatus(cardapp);
                return RedirectToAction("Contract");
            }
            return View();
        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public JsonResult DeleteCardApply(int CardApplyID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            CardBLL bll = new CardBLL();
            CardApplyModels card = new CardApplyModels();
            card = bll.getcontractbyid(CardApplyID);
            if (card != null)
            {
                bll.DeleteCardApply(card.CardApplyID);
                var result = new { Status = "OK" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
            {
                var result = new { Status = "Xóa thất bại" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }

        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateCard(int CardID)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            CardBLL cardbll = new CardBLL();
            List<CardProperties> list = new List<CardProperties>();
            List<CardCategoryModels> cate = new List<CardCategoryModels>();
            CompanyBLL bll = new CompanyBLL();
            CreditCard card = new CreditCard();
            List<CompanyModels> com = new List<CompanyModels>();

            list = cardbll.getCardPropertiesList(lang.LangID);
            card = cardbll.getCreditCardByIDManager(CardID, lang.LangID);
            com = bll.getCompany(lang.LangID);

            Models.CreditCard c = new Models.CreditCard();
            ViewBag.CardPropertiesListID = new SelectList(list, "CardPropertiesID", "CardPropertiesName", card.CardPropertiesID);
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name", card.CompanyID);
            c.CardName = card.CardName;
            c.CardImage = card.CardImage;
            c.CardPropertiesID = card.CardPropertiesID;
            c.BenefitContent = WebUtility.HtmlDecode(card.BenefitContent);
            c.FurtureContent = WebUtility.HtmlDecode(card.FurtureContent);
            c.CompanyID = card.CompanyID;
            c.Description = WebUtility.HtmlDecode(card.Description);
            c.MoreBenefit = WebUtility.HtmlDecode(card.MoreBenefit);
            c.MoreFee = WebUtility.HtmlDecode(card.MoreFee);
            c.MoreInfo = WebUtility.HtmlDecode(card.MoreInfo);
            string coststr = String.Format("{0:0,0}", card.Cost);
            c.Cost = coststr.Replace(",", ".");
            c.Interest = card.Interest;
            c.Display = card.Display;
            c.CardID = card.CardID;
            return View(c);
        }
        [ValidateInput(false)]
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult UpdateCard(FO.Models.CreditCard card)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            CardBLL cardbll = new CardBLL();
            List<CardProperties> list = new List<CardProperties>();
            List<CardCategoryModels> cate = new List<CardCategoryModels>();
            CompanyBLL bll = new CompanyBLL();
            CreditCard ca = new CreditCard();
            List<CompanyModels> com = new List<CompanyModels>();
            list = cardbll.getCardPropertiesList(lang.LangID);
            ca = cardbll.getCreditCardByIDManager(card.CardID, lang.LangID);
            com = bll.getCompany(lang.LangID);
            ViewBag.CardPropertiesListID = new SelectList(list, "CardPropertiesID", "CardPropertiesName", ca.CardPropertiesID);
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name", ca.CompanyID);
            if (ca != null)
            {
                ca.CardName = card.CardName;
                ca.CardImage = card.CardImage;
                ca.CardPropertiesID = card.CardPropertiesID;
                ca.BenefitContent = WebUtility.HtmlEncode(card.BenefitContent);
                ca.FurtureContent = WebUtility.HtmlEncode(card.FurtureContent);
                ca.CompanyID = card.CompanyID;
                ca.Description = WebUtility.HtmlEncode(card.Description);
                ca.MoreBenefit = WebUtility.HtmlEncode(card.MoreBenefit);
                ca.MoreFee = WebUtility.HtmlEncode(card.MoreFee);
                ca.MoreInfo = WebUtility.HtmlEncode(card.MoreInfo);
                ca.Cost = double.Parse(card.Cost.Replace(".", "").Replace(".", ""));
                ca.Interest = card.Interest;
                ca.Display = card.Display;
                cardbll.UpdateCard(ca, lang.LangID);

                return RedirectToAction("ManagerFuelCard");
            }
            return View();


        }
        [HttpGet]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertCard()
        {
            CardBLL cardbll = new CardBLL();
            List<CardProperties> list = new List<CardProperties>();
            List<CardCategoryModels> cate = new List<CardCategoryModels>();
            CompanyBLL bll = new CompanyBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            list = cardbll.getCardPropertiesList(lang.LangID);
            com = bll.getCompany(lang.LangID);
            ViewBag.CardPropertiesListID = new SelectList(list, "CardPropertiesID", "CardPropertiesName");
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name");
            return View();

        }
        [HttpPost]
        [Authorize(Roles = "8,9")]
        public ActionResult InsertCard(FO.Models.CardInsert card)
        {
            LanguageBLL langbll = new LanguageBLL();
            Language lang = langbll.getLangByCode(Request.Cookies["_culture"]["lang"]);
            CardBLL cardbll = new CardBLL();
            List<CardProperties> list = new List<CardProperties>();
            List<CardCategoryModels> cate = new List<CardCategoryModels>();
            CompanyBLL bll = new CompanyBLL();
            List<CompanyModels> com = new List<CompanyModels>();
            list = cardbll.getCardPropertiesList(lang.LangID);
            com = bll.getCompany(lang.LangID);
            ViewBag.CardPropertiesListID = new SelectList(list, "CardPropertiesID", "CardPropertiesName");
            ViewBag.CompanyListID = new SelectList(com, "CompanyID", "Name");
            if (ModelState.IsValid)
            {
                CreditCard items = new CreditCard();
                CreditCard itemsLang = new CreditCard();
                items.CardName = card.CardName;
                items.CardImage = card.CardImage;
                items.CardPropertiesID = card.CardPropertiesID;
                items.CardCategoryID = 2;
                items.BenefitContent = WebUtility.HtmlEncode(card.BenefitContent);
                items.FurtureContent = WebUtility.HtmlEncode(card.FurtureContent);
                items.CompanyID = card.CompanyID;
                items.Description = WebUtility.HtmlEncode(card.Description);
                items.MoreBenefit = WebUtility.HtmlEncode(card.MoreBenefit);
                items.MoreFee = WebUtility.HtmlEncode(card.MoreFee);
                items.MoreInfo = WebUtility.HtmlEncode(card.MoreInfo);
                items.Cost = double.Parse(card.Cost.Replace(".", "").Replace(".", ""));
                items.Interest = card.Interest;
                items.Display = false;
                ////
                itemsLang.CardName = card.CardNameLang;
                itemsLang.CardImage = card.CardImage;
                itemsLang.CardPropertiesID = card.CardPropertiesID;
                itemsLang.CardCategoryID = 2;
                itemsLang.BenefitContent = WebUtility.HtmlEncode(card.BenefitContentLang);
                itemsLang.FurtureContent = WebUtility.HtmlEncode(card.FurtureContentLang);
                itemsLang.CompanyID = card.CompanyID;
                itemsLang.Description = WebUtility.HtmlEncode(card.DescriptionLang);
                itemsLang.MoreBenefit = WebUtility.HtmlEncode(card.MoreBenefitLang);
                itemsLang.MoreFee = WebUtility.HtmlEncode(card.MoreFeeLang);
                itemsLang.MoreInfo = WebUtility.HtmlEncode(card.MoreInfoLang);
                itemsLang.Cost = double.Parse(card.Cost.Replace(".", "").Replace(".", ""));
                itemsLang.Interest = card.Interest;
                itemsLang.Display = false;
                cardbll.InsertCard(items);
                CreditCard carditem = cardbll.getTop1Card();
                items.CardID = carditem.CardID;
                itemsLang.CardID = carditem.CardID;
                cardbll.InsertCardLang(items, 1);
                cardbll.InsertCardLang(itemsLang, 2);
                return RedirectToAction("ManagerFuelCard");
            }
            return View(card);

        }

        public ActionResult ThanksYou()
        {
            return View();
        }
        [HttpPost]
        public ActionResult SendEmail(string Name, string phone, string emailuser, int CardID)
        {
            try
            {
                string MailFromUserName = ConfigurationManager.AppSettings["MailFromUserName"].ToString();
                string MailFromPassword = ConfigurationManager.AppSettings["MailFromPassword"].ToString();
                string MailSMTP = ConfigurationManager.AppSettings["MailSMTP"].ToString();
                string MailCC1 = ConfigurationManager.AppSettings["MailCC1"].ToString();
                string MailCC2 = ConfigurationManager.AppSettings["MailCC2"].ToString();
                string MailSendTo = ConfigurationManager.AppSettings["MailSendTo"].ToString();
                Models.EmailSend email = new Models.EmailSend();
                email.EmailSendTo = MailSendTo;
                email.Subject = "Khách hàng " + Name + " Apply Thẻ xăng, dầu ID: " + CardID + "";
                email.Body = "";
                email.Body += "<p>Hi, Admin</p>";
                email.Body += "<p>Khách hàng: " + Name + " Apply Thẻ xăng dầu ID: " + CardID + "</p>";
                email.Body += "<p>Email: " + emailuser + "</p>";
                email.Body += "<p>SĐT: " + phone + "</p>";
                email.Body += "<p><a href='http://www.vnbanknet.com/SavingFuelCard/ContractDetail/" + CardID + "'>Link chi tiết</a></p>";
                email.Body += "<p>Thanks</p>";
                MailMessage message = new MailMessage();
                message.Body = email.Body;
                message.IsBodyHtml = true;
                message.From = new System.Net.Mail.MailAddress(MailFromUserName);
                message.To.Add(email.EmailSendTo);
                message.CC.Add(MailCC1);
                message.CC.Add(MailCC2);
                message.Subject = email.Subject;

                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(MailSMTP);// you need an smtp server address to send emails
                smtp.Credentials = new System.Net.NetworkCredential(MailFromUserName, MailFromPassword); //missing line from ur code
                smtp.Port = 587;  //missing line from your code
                smtp.EnableSsl = true;   //missing line from your code
                smtp.Send(message);
                int result = 1;
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch
            {
                int result = -1;
                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }

    }
}
